<?php
namespace app\model;
use app\utils\CamlHump;
use think\facade\Db;
use think\Model;
class SysDepartment extends BaseModel
{
    protected $convertNameToCamel = true;
    protected $name = 'sys_department';

    static function  build($data)
    {
        $emptyData = [];


        //是int类型的 BIGINT
        $emptyData['id']= $data['id' ] ?? 0;






        //是 VARCHAR 类型的 VARCHAR
        $emptyData['code']= $data['code' ] ?? '';





        //是 VARCHAR 类型的 VARCHAR
        $emptyData['name']= $data['name' ] ?? '';




        //是int类型的 BIGINT
        $emptyData['parentId']= $data['parentId' ] ?? 0;






        //是 VARCHAR 类型的 VARCHAR
        $emptyData['parentIdPath']= $data['parentIdPath' ] ?? '';





        //是 VARCHAR 类型的 VARCHAR
        $emptyData['description']= $data['description' ] ?? '';





        //是 VARCHAR 类型的 VARCHAR
        $emptyData['img']= $data['img' ] ?? '';




        //是int类型的 SMALLINT
        $emptyData['deep']= $data['deep' ] ?? 0;





        //是int类型的 BIGINT
        $emptyData['orderNum']= $data['orderNum' ] ?? 0;





        //是int类型的 TINYINT
        $emptyData['departType']= $data['departType' ] ?? 0;






        //是 VARCHAR 类型的 VARCHAR
        $emptyData['typeInfo']= $data['typeInfo' ] ?? '';




        //是int类型的 BIGINT
        $emptyData['dingDeptId']= $data['dingDeptId' ] ?? 0;





        //是int类型的 TINYINT
        $emptyData['tenantType']= $data['tenantType' ] ?? 0;





        //是int类型的 BIGINT
        $emptyData['tenantId']= $data['tenantId' ] ?? 0;





        //是int类型的 TINYINT
        $emptyData['delState']= $data['delState' ] ?? 0;






        //是 VARCHAR 类型的 VARCHAR
        $emptyData['addUser']= $data['addUser' ] ?? '';




        //是int类型的 BIGINT
        $emptyData['addUserId']= $data['addUserId' ] ?? 0;







        //是 VARCHAR 类型的 DATETIME
        $emptyData['addTime']= $data['addTime' ] ?? date("Y-m-d H:i:s", time());




        //是 VARCHAR 类型的 VARCHAR
        $emptyData['editUser']= $data['editUser' ] ?? '';




        //是int类型的 BIGINT
        $emptyData['editUserId']= $data['editUserId' ] ?? 0;







        //是 VARCHAR 类型的 DATETIME
        $emptyData['lastEditTime']= $data['lastEditTime' ] ?? date("Y-m-d H:i:s", time());



        //是int类型的 SMALLINT
        $emptyData['serverNumber']= $data['serverNumber' ] ?? 0;



        return $emptyData;
    }
    static function createQuery($queryData)
    {
        $query = Db::table('sys_department');

        //是int类型的 BIGINT
        if(isset($queryData['id'])&&$queryData['id']>0){
            $query->where('id', $queryData['id']);
        }



        //是 VARCHAR 类型的 VARCHAR
        if(isset($queryData['code'])&&$queryData['code']!=''){
            $query->where('code', $queryData['code']);
        }



        //是 VARCHAR 类型的 VARCHAR
        if(isset($queryData['name'])&&$queryData['name']!=''){
            $query->where('name', $queryData['name']);
        }


        //是int类型的 BIGINT
        if(isset($queryData['parentId'])&&$queryData['parentId']>=0){
            $query->where('parent_id', $queryData['parentId']);
        }



        //是 VARCHAR 类型的 VARCHAR
        if(isset($queryData['parentIdPath'])&&$queryData['parentIdPath']!=''){
            $query->where('parent_id_path', $queryData['parentIdPath']);
        }



        //是 VARCHAR 类型的 VARCHAR
        if(isset($queryData['description'])&&$queryData['description']!=''){
            $query->where('description', $queryData['description']);
        }



        //是 VARCHAR 类型的 VARCHAR
        if(isset($queryData['img'])&&$queryData['img']!=''){
            $query->where('img', $queryData['img']);
        }


        //是int类型的 SMALLINT
        if(isset($queryData['deep'])&&$queryData['deep']>0){
            $query->where('deep', $queryData['deep']);
        }



        //是int类型的 BIGINT
        if(isset($queryData['orderNum'])&&$queryData['orderNum']>0){
            $query->where('order_num', $queryData['orderNum']);
        }



        //是int类型的 TINYINT
        if(isset($queryData['departType'])&&$queryData['departType']>0){
            $query->where('depart_type', $queryData['departType']);
        }



        //是 VARCHAR 类型的 VARCHAR
        if(isset($queryData['typeInfo'])&&$queryData['typeInfo']!=''){
            $query->where('type_info', $queryData['typeInfo']);
        }


        //是int类型的 BIGINT
        if(isset($queryData['dingDeptId'])&&$queryData['dingDeptId']>0){
            $query->where('ding_dept_id', $queryData['dingDeptId']);
        }



        //是int类型的 TINYINT
        if(isset($queryData['tenantType'])&&$queryData['tenantType']>0){
            $query->where('tenant_type', $queryData['tenantType']);
        }



        //是int类型的 BIGINT
        if(isset($queryData['tenantId'])&&$queryData['tenantId']>0){
            $query->where('tenant_id', $queryData['tenantId']);
        }



        //是int类型的 TINYINT
        if(isset($queryData['delState'])&&$queryData['delState']>0){
            $query->where('del_state', $queryData['delState']);
        }



        //是 VARCHAR 类型的 VARCHAR
        if(isset($queryData['addUser'])&&$queryData['addUser']!=''){
            $query->where('add_user', $queryData['addUser']);
        }


        //是int类型的 BIGINT
        if(isset($queryData['addUserId'])&&$queryData['addUserId']>0){
            $query->where('add_user_id', $queryData['addUserId']);
        }




        //是 VARCHAR 类型的 DATETIME
        if(isset($queryData['addTime'])&&$queryData['addTime']!=''){
            $query->where('add_time', $queryData['addTime']);
        }


        //是 VARCHAR 类型的 VARCHAR
        if(isset($queryData['editUser'])&&$queryData['editUser']!=''){
            $query->where('edit_user', $queryData['editUser']);
        }


        //是int类型的 BIGINT
        if(isset($queryData['editUserId'])&&$queryData['editUserId']>0){
            $query->where('edit_user_id', $queryData['editUserId']);
        }




        //是 VARCHAR 类型的 DATETIME
        if(isset($queryData['lastEditTime'])&&$queryData['lastEditTime']!=''){
            $query->where('last_edit_time', $queryData['lastEditTime']);
        }

        //是int类型的 SMALLINT
        if(isset($queryData['serverNumber'])&&$queryData['serverNumber']>0){
            $query->where('server_number', $queryData['serverNumber']);
        }



        $query->where('del_state', 1);

        return   $query;

    }




    static function addPlus($data){
        $addWidthDefault = static::build($data);
        $result = static::create($addWidthDefault);
        return $result;
    }
    static function updatePlus($data, $id)
    {
        $result = static::update($data, ['id' => $id]);
        return $result;
    }
    static function deletePlus($id)
    {
        $result = static::destroy($id);
        return $result;
    }

    static function findOne($id){
        $query=Db::table('sys_department')->where("id", $id);
        $one = CamlHump::toCamlObj($query->find());
        return $one;
    }

    public static function haveChildByParentId($parentId)
    {
        if($parentId<=0){
            return false;
        }
        $query=Db::table('sys_department')->where("parent_id", $parentId)
            ->where("del_state", 1);
    return $query->count()>0;
    }
}